package org.jctc.els.modules.biz.service.projects.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jctc.els.common.constant.BaseRoleConstant;
import org.jctc.els.modules.biz.entity.DictBiz;
import org.jctc.els.modules.biz.entity.projects.ElsProjectReportGouzhi;
import org.jctc.els.modules.biz.entity.projects.ElsProjectReportJigai;
import org.jctc.els.modules.biz.entity.projects.ElsProjects;
import org.jctc.els.modules.biz.entity.projects.ElsReportGouzhiJfys;
import org.jctc.els.modules.biz.excel.ElsReportGouzhiJfysExcel;
import org.jctc.els.modules.biz.mapper.projects.ElsProjectReportGouzhiMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsReportGouzhiJfysMapper;
import org.jctc.els.modules.biz.service.projects.IElsProjectReportGouzhiService;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BaseRoleUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Service
public class ElsProjectReportGouzhiServiceImpl implements IElsProjectReportGouzhiService {

    @Autowired
    private ElsProjectReportGouzhiMapper elsProjectReportGouzhiMapper;
    @Autowired
    private ElsReportGouzhiJfysMapper elsReportGouzhiJfysMapper;
    @Override
    public ElsProjectReportGouzhi getProjectId(Long projectId) {
        return elsProjectReportGouzhiMapper.selectOne(new QueryWrapper<ElsProjectReportGouzhi>().eq("project_id",projectId));
    }

    @Override
    public Integer save(ElsProjectReportGouzhi reportGouzhi) {
        return elsProjectReportGouzhiMapper.insert(reportGouzhi);
    }

    @Override
    public Integer update(ElsProjectReportGouzhi reportGouzhi) {
        return elsProjectReportGouzhiMapper.updateById(reportGouzhi);
    }

    @Override
    public R importPurchaseProjects(MultipartFile multipartFile, HttpServletRequest request, String gouZhiId) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<ElsReportGouzhiJfysExcel> data = ExcelUtil.read(multipartFile, ElsReportGouzhiJfysExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        List<ElsReportGouzhiJfys> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            ElsReportGouzhiJfysExcel gouzhiJfysExcel = data.get(i);
            String name = gouzhiJfysExcel.getName();
            if(StrUtil.isEmpty(name)){
                throw new RuntimeException("名称不可为空！");
            }
            String ggjxh = gouzhiJfysExcel.getGgjxh();
            if(StrUtil.isEmpty(ggjxh)){
                throw new RuntimeException("名称为："+name+"的规格及型号不可为空！");
            }
            Integer sl = gouzhiJfysExcel.getSl();
            if(sl==null){
                throw new RuntimeException("名称为："+name+"的数量不可为空！");
            }
            Double dj = gouzhiJfysExcel.getDj();
            if (dj == null) {
                throw new RuntimeException("名称为："+name+"的单价不可为空！");
            }
            Double zj = gouzhiJfysExcel.getZj();
            if (zj == null) {
                throw new RuntimeException("名称为："+name+"的总价不可为空！");
            }
            String djyj = gouzhiJfysExcel.getDjyj();
            if(StrUtil.isEmpty(djyj)){
                throw new RuntimeException("名称为："+name+"的单价依据不可为空！");
            }
//            ElsProjectReportGouzhi projectId = elsProjectReportGouzhiMapper.selectOne(new QueryWrapper<ElsProjectReportGouzhi>().eq("project_id", gouZhiId));
//            elsReportGouzhiJfysMapper.deleteByIds(Long.valueOf(gouZhiId));
            ElsReportGouzhiJfys elsReportGouzhiJfys = new ElsReportGouzhiJfys();
            elsReportGouzhiJfys.setId(Long.valueOf(gouZhiId));
            elsReportGouzhiJfys.setName(name);
            elsReportGouzhiJfys.setGgjxh(ggjxh);
            elsReportGouzhiJfys.setDj(dj);
            elsReportGouzhiJfys.setSl(sl);
            elsReportGouzhiJfys.setZj(zj);
            elsReportGouzhiJfys.setDjyj(djyj);
            elsReportGouzhiJfysMapper.insert(elsReportGouzhiJfys);
            list.add(elsReportGouzhiJfys);
        }
        return R.data(list);
    }

    @Override
    public List<ElsReportGouzhiJfys> getJfysId(String id) {
        return elsReportGouzhiJfysMapper.selectList(new QueryWrapper<ElsReportGouzhiJfys>().eq("id",id));
    }
}
